<?php
namespace app\backend\controller;

use app\backend\model\Stage as StageModel;
use app\backend\model\Lists as ListsModel;
use app\backend\model\Tab as TabModel;
use app\backend\model\Menu as MenuModel;
use think\Request;
use think\Db;
use app\backend\org\Image;

class  Stage  extends Base
{
    //新闻段落新建
    public function stage_create($type=""){
        if (Request::instance()->isPost()){
            $model = new StageModel;
            $post = Request::instance()->post();// 获取经过过滤的全部post变量
            $post["content_cn"]=preg_replace('/<p><br\/><\/p>/','<br/>',$post["content_cn"]);
            $post["content_en"]=preg_replace('/<p><br\/><\/p>/','<br/>',$post["content_en"]);
           // var_dump($post);exit;
            if(!$post["news_time"]){
                $post["news_time"]=date("Y-m-d");
            }

            // 判断已有内容不能重复新增去修改
            $name = Db::query("select stage_id,stage_title_cn from ls_stage where `p_id`='".$post['p_id']."' and `stage_title_cn`='".$post['stage_title_cn']."' and delete_time is NULL ");
            $list_id = Request::instance()->param("list_id");
            //var_dump($name);exit;
            if(!empty($name)){
                return $this->redirect(url('stage_update',array('type'=>$type,'list_id'=>$list_id)));
            }

            // 验证表单并插入数据库
            $result = $this->validate($post,'Stage.add');
            if(true !== $result){
                // 验证失败 输出错误信息
                dump($result);
                return $this->error($msg = '参数错误，验证失败');
            }

            // 登录人的ID
            $post["creator_id"] = \think\Session::get('login_id');

            // 跳转到显示页面
            // 插入数据库后修改数据库跳转连接
            $line = $model->save($post);
            //$list_id = Request::instance()->param("list_id");
            $tab_id = Db::query("select p_id from ls_list where list_id = $list_id");
            $id = Db::query("select p_id from ls_tab where tab_id = {$tab_id[0]['p_id']}");
            $type = Request::instance()->param("type");
            //var_dump($model);
            //var_dump();die();
            if(!$line){
                return $this->error($this->update_error);
            } else {
                return $this->success("添加内容成功",url('lists/index', ["type"=>$type,"id"=>$id[0]["p_id"]]) . '?tab_id='.$tab_id[0]["p_id"]);
            }
        }
        $type = Request::instance()->param("type");
        if($type == "news"){
            $this->assign('jsController','news');
            $type_id = 1;
            $this->assign('type_id',$type_id);
        }elseif ($type == "articles"){
            $this->assign('jsController','articles');
            $type_id = 2;
            $this->assign('type_id',$type_id);
        }
        $this->assign('type',$type);
        $list_id = Request::instance()->param("list_id");
        $this->assign('list_id',$list_id);
        $this->assign('p_id',$list_id);
        return $this->fetch();
    }

    public function stage_update($list_id){

        if (Request::instance()->isPost()){
            $model = new StageModel;
            $post = Request::instance()->post();// 获取经过过滤的全部post变量
            if(!$post["news_time"]){
                $post["news_time"]=date("Y-m-d h:i:s");
            }
            $post["content_cn"]=preg_replace('/<p><br\/><\/p>/','<br/>',$post["content_cn"]);
            $post["content_en"]=preg_replace('/<p><br\/><\/p>/','<br/>',$post["content_en"]);

            //var_dump($post);exit;

            // 验证表单并插入数据库
            $result = $this->validate($post,'Stage.add');
            if(!$result){
                $this->error($result->getError());
            }
            // 插入数据库后修改数据库跳转连接
            // 过滤掉数据表中没有的字段，保存
            $stage_id = Request::instance()->param("stage_id");
            $line = $model->allowField(true)->save($post,['stage_id'=>$stage_id]);
           // $line = $model->save($post);

            //$list_id = Db::query("select p_id from ls_stage where stage_id = $stage_id");
            $tab_id = Db::query("select p_id from ls_list where list_id = $list_id");
            $id = Db::query("select p_id from ls_tab where tab_id = {$tab_id[0]['p_id']}");
            $type = Request::instance()->param("type");
            //var_dump($model);
           // var_dump($type);die();
            if(!$line){
                return $this->error($this->update_error);
            } else {
                return $this->redirect(url('lists/index', ["type"=>$type,"id"=>$id[0]["p_id"]]) . '?tab_id='.$tab_id[0]["p_id"]);
            }
        }
        $type = Request::instance()->param("type");
        $stage_id = StageModel::where(["p_id"=>$list_id,"delete_time"=>["exp","is null"]])->value("stage_id");
        if(!isset($stage_id)){
            return $this->redirect(url('stage_create',array('type'=>$type,'list_id'=>$list_id)));
        }
        //var_dump($list_id);
        //var_dump($stage_id);exit;

        //var_dump($type);die();
        if($type == "news"){
            $this->assign('jsController','news');
            $type_id = 1;
            $this->assign('type_id',$type_id);
        }elseif ($type == "articles"){
            $this->assign('jsController','articles');
            $type_id = 2;
            $this->assign('type_id',$type_id);
        }

        $record = StageModel::get($stage_id);

//        echo $record["content_cn"];die;
        $record["content_cn"]=preg_replace('/[\r\n]/','',$record["content_cn"]);
        $record["content_en"]=preg_replace('/[\r\n]/','',$record["content_en"]);

        //var_dump($record);exit;
        $this->assign('type',$type);
      // $list_id = Request::instance()->param("list_id");
        $this->assign('list_id',$list_id);
        $this->assign('stage_id',$stage_id);
        $this->assign('stage',$record);
        //$this->assign('p_id',$list_id);
        return $this->fetch();
    }

    public function stage_delete($list_id){
            $stage_id = StageModel::where(["p_id"=>$list_id,"delete_time"=>["exp","is null"]])->value("stage_id");
        if(!isset($stage_id)){
            return $this->error("没有数据");
        }
            StageModel::destroy($stage_id);
            //$id = Request::instance()->param("id");
            $type = Request::instance()->param("type");
            $tab_id = Db::query("select p_id from ls_list where list_id = $list_id");
            $id = Db::query("select p_id from ls_tab where tab_id = {$tab_id[0]['p_id']}");
            //exit;
            $this->redirect(url('lists/index', ["type"=>$type,"id"=>$id[0]["p_id"]]) . '?tab_id='.$tab_id[0]["p_id"]);

    }


}